<?php


/**
* CakePHP Routines Plugin
* http://cake-routines-plugin.googlecode.com
*
* @package routines
* @subpackage routines.models.behaviors
* @author Ivan Chistyakov <zetruger@gmail.com>
* @version $Id: routinable.php 170 2010-10-22 10:48:56Z zetruger@gmail.com $
* @link $HeadURL: http://cake-routines-plugin.googlecode.com/svn/trunk/models/behaviors/routinable.php $
* @license MIT License (http://opensource.org/licenses/mit-license.php)
*/


class RoutinableBehavior extends ModelBehavior {

var $_defaults = array(
	'slugField' => 'slug',
	'dividField' => 'divider',
	'attachModelField' => 'model',
	'attachForeignKeyField' => 'foreign_key',
	'sortField' => 'position',
	'hideField' => 'disabled'
);

var $settings = array();

function setup(&$Model, $settings = array()) {
	$this->settings[$Model->alias] = $settings;
	extract($this->_getSettings($Model), EXTR_SKIP);
	if (!$Model->Behaviors->attached('Validable')) {
		//$Model->Behaviors->attach('Routines.Validable', $settings);
	}
	if (!$Model->Behaviors->attached('Scopable')) {
		//$Model->Behaviors->attach('Routines.Scopable', $settings);
	}
	if (!$Model->Behaviors->attached('Associable')) {
		//$Model->Behaviors->attach('Routines.Associable', $settings);
	}
	if (!$Model->Behaviors->attached('Neighborable')) {
		$Model->Behaviors->attach('Routines.Neighborable', $settings);
	}
	if ($Model->hasField($slugField) && !$Model->Behaviors->attached('Slugable')) {
		$Model->Behaviors->attach('Routines.Slugable', $settings);
	}
	if ($Model->hasField($dividField) && !$Model->Behaviors->attached('Dividable')) {
		$Model->Behaviors->attach('Routines.Dividable', $settings);
	}
	if ($Model->hasField(array($attachModelField, $attachForeignKeyField)) && !$Model->Behaviors->attached('Attachable')) {
		$Model->Behaviors->attach('Routines.Attachable', $settings);
	}
	if ($Model->hasField($sortField) && !$Model->Behaviors->attached('Sortable')) {
		$Model->Behaviors->attach('Routines.Sortable', $settings);
	}
	if ($Model->hasField($hideField) && !$Model->Behaviors->attached('Hidable')) {
		$Model->Behaviors->attach('Routines.Hidable', $settings);
	}
	if (!$Model->Behaviors->attached('Forbidable')) {
		$Model->Behaviors->attach('Routines.Forbidable', $settings);
	}
}

function _getSettings(&$Model, $settings = array()) {
	$settings = Set::merge($this->settings[$Model->alias], $settings);
	if (isset($settings['noUseModelVars']) && $settings['noUseModelVars']) {
		return Set::merge($this->_defaults, $settings);
	} else {
		return Set::merge($this->_defaults, array_intersect_key(get_object_vars($Model), $this->_defaults), $settings);
	}
}

}


?>